package com.bestcem.xm.award.dao.impl.mysql;

import com.bestcem.xm.award.dao.AwardPointStatsDao;
import com.bestcem.xm.award.dao.mapper.AwardPointStatsMapper;
import com.bestcem.xm.award.entity.mysql.AwardPointStatsEntity;
import com.bestcem.xm.award.entity.pojo.PointStatsCacheDO;
import com.bestcem.xm.common.core.uitls.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author xa.zhang <xa.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/10/21 11:31
 */
@Repository
public class AwardPointStatsDaoImpl implements AwardPointStatsDao {

    @Resource
    private AwardPointStatsMapper pointStatsMapper;

    @Override
    public PointStatsCacheDO selectByAwardId(String awardId) {
        AwardPointStatsEntity entity = pointStatsMapper.selectByPrimaryKey(awardId);
        if (entity == null) {
            return null;
        }
        PointStatsCacheDO pointStatsCacheDO = new PointStatsCacheDO();
        BeanUtils.copyProperties(entity, pointStatsCacheDO);

        return pointStatsCacheDO;
    }

    @Override
    public int insert(String awardId, long count, long amount) {
        AwardPointStatsEntity entity = new AwardPointStatsEntity();
        entity.setAwardId(awardId);
        entity.setAmount(amount);
        entity.setCount(count);
        entity.setCreateTime(DateUtil.getCommonDate());
        entity.setUpdateTime(DateUtil.getCommonDate());

        return pointStatsMapper.insert(entity);
    }

    @Override
    public int addCountAndAmountByAwardId(String awardId, long count, long amount) {
        AwardPointStatsEntity entity = pointStatsMapper.selectByPrimaryKey(awardId);
        if (entity == null) {
            return insert(awardId, count, amount);
        } else {
            return pointStatsMapper.addCountAndAmountByAwardId(awardId, count, amount, DateUtil.getCommonDate());
        }

    }
}
